// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import appConfig from "../../framework/appConfig";

const {ccclass, property} = cc._decorator;

let refle_Java_url = "org/cocos2dx/javascript/SDKManager/Manager";

@ccclass
export class SDKManager{

    // onLoad () {}

    static successCallback : Function;
    static failCallback : Function;
    static payCallback : Function;
    static notificationCallback : Function;

    //=======================================================JS 调用JAVA相关代码===========================================================

    /**
     * 播放激励视频广告的方法  方法中只需要调用这个
     *
     * @param sceneName ⼴告场景，开发者⾃定义，⽤于统计不同场景的⼴告表现
     * @param showSuccess 广告播放成功的回调
     * @param showFail 广告播放失败的回调
     */
    public static showRewardVideo(sceneName,showSuccess,showFail){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            showSuccess();
        }else{
            this.successCallback = showSuccess;
            this.failCallback = showFail;
            jsb.reflection.callStaticMethod(refle_Java_url,"showVideo","(Ljava/lang/String;)V",sceneName);
        }
    }

    /**
     * 播放插屏的方法  方法中只需要调用这个
     *
     * @param sceneName ⼴告场景，开发者⾃定义，⽤于统计不同场景的⼴告表现
     * @param showSuccess 广告播放成功的回调
     * @param showFail 广告播放失败的回调
     * 说明 失败的回调表示 展示失败，可以执⾏进⼊应⽤逻辑  成功的回调表示已经成功展示完 隐藏了
     */
    public static showInterstitial(sceneName,showSuccess,showFail){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            showSuccess();
        }else{
            this.successCallback = showSuccess;
            this.failCallback = showFail;
            jsb.reflection.callStaticMethod(refle_Java_url,"showInterstitial","(Ljava/lang/String;)V",sceneName);
        }
    }

    /**
     * 播放开屏广告的方法  方法中只需要调用这个
     *
     * @param sceneName ⼴告场景，开发者⾃定义，⽤于统计不同场景的⼴告表现
     * @param showSuccess 广告播放成功的回调
     * @param showFail 广告播放失败的回调
     * 说明 失败的回调表示 展示失败，可以执⾏进⼊应⽤逻辑  成功的回调表示已经成功展示完 隐藏了
     */
    public static showAppOpen(sceneName,showSuccess,showFail){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            showSuccess();
        }else{
            this.successCallback = showSuccess;
            this.failCallback = showFail;
            jsb.reflection.callStaticMethod(refle_Java_url,"showAppOpen","(Ljava/lang/String;)V",sceneName);
        }
    }

    /**
     * 播放原生广告的方法  方法中只需要调用这个
     *
     * @param sceneName ⼴告场景，开发者⾃定义，⽤于统计不同场景的⼴告表现
     * @param showSuccess 广告播放成功的回调
     * @param showFail 广告播放失败的回调
     * 说明 失败的回调表示 展示失败，可以执⾏进⼊应⽤逻辑  成功的回调表示已经成功展示完 隐藏了
     */
    public static showNativeAd(sceneName,showSuccess,showFail){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            showSuccess();
        }else{
            this.successCallback = showSuccess;
            this.failCallback = showFail;
            jsb.reflection.callStaticMethod(refle_Java_url,"showNativeAd","(Ljava/lang/String;)V",sceneName);
        }
    }

    /**
     * 销毁原生广告的方法  方法中只需要调用这个
     */
    public static destroyNativeAd(){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            console.log("摧毁广告");
        }else{
            jsb.reflection.callStaticMethod(refle_Java_url,"destroyNativeAd","()V");
        }

    }

    /**
     * 统计
     *
     * @param eventName 事件名
     */
    public static trackEvent(eventName){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            console.log("打点",eventName);
        }else{
            jsb.reflection.callStaticMethod(refle_Java_url,"trackEvent","(Ljava/lang/String;)V",eventName);
        }
    }

    /**
     * 即时统计
     *
     * @param args 键值名
     */
    public static trackImmediatelyEvent(eventName,...args){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            console.log("打点",eventName,...args);
        }else{
            console.log("参数类型",args,typeof JSON.stringify(args),JSON.stringify(args));
            jsb.reflection.callStaticMethod(refle_Java_url,"trackImmediately","(Ljava/lang/String;Ljava/lang/String;)V",eventName,JSON.stringify(args));
        }
    }

    /**
     * 项目中主动调用申请提现
     *
     * @param paymentType ⽀付⽅式 Paypal,payerMax-PagBank,payerMax-DANA等等中任⼀种
     * @param account 收款账号
     * @param name ⽤户名，只有OVO 提现⽅式需要，其他为""
     * @param amount 提现数额
     */
    public static pay(paymentType,account,name,amount,callback){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            callback();
        }else{
            jsb.reflection.callStaticMethod(refle_Java_url,"pay","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;F)V",paymentType,account,name,amount);
            this.payCallback = callback;
        }
    }

    /**
     * 添加定时推送
     *
     * @param uniqueId 唯⼀标识，可以是随机字符串,同个标识再次调⽤会刷新
     * @param delay 延迟⼏秒发送，单位（秒）
     * @param title 推送标题
     * @param body 推送正⽂
     * @param data 数据，可为空
     * @param callback 回调，可为空
     */
    public static addNotification(uniqueId,delay,title,body,data,callback){
        if(cc.sys.os == "Windows" || appConfig.gamePlatform() == 0){
            callback();
        }else{
            jsb.reflection.callStaticMethod(refle_Java_url,"addNotification","(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",uniqueId,delay,title,body,data);
            this.notificationCallback = callback;
        }
    }

    //===========================================================JAVA 调用JS相关代码==========================================================

    /**
     * 项目中主动调用定时推送回调
     */
    public static getNotificationCallback(msg){
        let index = msg.indexOf("not");
        let data = msg.slice(0,index + 1);
        let isPay = msg.slice(index + 3,msg.length);
        this.payCallback(isPay,JSON.parse(data));
    }

    /**
     * 项目中主动调用申请提现回调
     */
    public static getPayCallback(msg){
        let index = msg.indexOf("pay");
        let data = msg.slice(0,index + 1);
        let isPay = msg.slice(index + 3,msg.length);
        this.payCallback(isPay,JSON.parse(data));
    }

    /**
     * 所有广告的回调方法  激励视频、插屏、开屏
     */
    public static showRewardCallback(code){
        if(code == 0){
            this.successCallback();
        }else{
            this.failCallback();
        }
    }

    /**
     * 获取远程配置
     *
     * @param id 配置
     */
    public static receiveRemoteData(msg){
        let data = JSON.parse(msg);
        console.log("远程配置",data);
    }

    /**
     * 获取远程策略
     *
     * @param msg 策略
     */
    public static receiveRemoteStrategy(msg){
        let data = JSON.parse(msg);
        console.log("远程策略",data);
    }

    // update (dt) {}
}

window["manager"] = SDKManager;


